package com.cdshangpai.photoapp.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.cdshangpai.photoapp.PhotoAppBaseActivity;
import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.adapters.DataBean;
import com.cdshangpai.photoapp.databinding.ActivityServiceBinding;
import com.cdshangpai.photoapp.ui.view.DragImageView;
import com.cdshangpai.utils.db.AboutusDb;
import com.cdshangpai.utils.db.beans.Aboutus;
import com.cdshangpai.utils.storage.ImageManager;
import com.cdshangpai.utils.system.ScreenParams;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class ServiceActivity extends PhotoAppBaseActivity {
    ActivityServiceBinding  binding;
    AboutusDb aboutusDb;
    Boolean menuOpened;
    List<ServiceMenuItemBean> menus;
    ServiceMenuAdapter serviceMenuAdapter;
    ImageManager imageManager;

    public OnFilterItemClickListener serviceMenuItemClick=new OnFilterItemClickListener() {
        @Override
        public void onItemClick(View view, String name, Integer position) {
            for(int i=0;i<binding.getAdapter().datas.size();i++)
            {
                if(i!=position&&binding.getAdapter().datas.get(i).iscurrent)
                {
                    binding.getAdapter().datas.get(i).iscurrent=false;
                    binding.getAdapter().notifyItemChanged(i);
                    break;
                }
            }
            binding.getAdapter().datas.get(position).iscurrent=true;
            binding.getAdapter().notifyItemChanged(position);
            setImageList();
        }
    };

    public void switchMenu()
    {
        menuOpened=!menuOpened;
    }
    public Boolean isMenuOpened()
    {
        return menuOpened;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        aboutusDb=new AboutusDb();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        syncInitData();
    }


    private void initView()
    {
        binding= DataBindingUtil.setContentView(this,R.layout.activity_service);
        binding.serviceMenuList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.setShowMenu(false);
        serviceMenuAdapter=new ServiceMenuAdapter(this.getApplicationContext());
        serviceMenuAdapter.onFilterItemClickListener=serviceMenuItemClick;
        binding.setAdapter(serviceMenuAdapter);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getApplicationContext());
        binding.serviceMenuList.setLayoutManager(linearLayoutManager);
        binding.setView(this);
        binding.setShowBigPic(false);
        DragImageView iv=(DragImageView)binding.serviceBigImage;
        iv.setmActivity(this);
        iv.setScreen_W(ScreenParams.screenWidth);
        iv.setScreen_H(ScreenParams.screenHeight);
        imageManager = new ImageManager();

        ((SimpleItemAnimator)binding.serviceMenuList.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    private void syncInitData()
    {
        new Runnable(){
            @Override
            public void run() {
                initData();
            }
        }.run();
    }

    /**
     * 初始化数据，生成菜单数据列表
     */
    private void initData()
    {
        menus=new ArrayList<>();
        Aboutus aboutus=aboutusDb.queryBean();
        if(aboutus!=null&&!TextUtils.isEmpty(aboutus.photos))
        {
            menus.add(new ServiceMenuItemBean("致新人的第一封信",true,string2List(aboutus.letterphotos)));
            menus.add(new ServiceMenuItemBean("公司介绍",false,string2List(aboutus.photos)));
//            menus.add(new ServiceMenuItemBean("婚礼理念",false,string2List(aboutus.ideaphotos)));
            menus.add(new ServiceMenuItemBean("服务内容",false,string2List(aboutus.servicephotos)));
            menus.add(new ServiceMenuItemBean("服务流程",false,string2List(aboutus.processphotos)));
            menus.add(new ServiceMenuItemBean("启蔻优势",false,string2List(aboutus.advantagephotos)));
            menus.add(new ServiceMenuItemBean("现场vs方案",false,string2List(aboutus.casephotos)));
            menus.add(new ServiceMenuItemBean("收费标准",false,string2List(aboutus.chargesphotos)));
//            menus.add(new ServiceMenuItemBean("近期优惠",false,string2List(aboutus.letterphotos)));
            menus.add(new ServiceMenuItemBean("相关文档",false,string2List(aboutus.documentphotos)));
        }
        binding.getAdapter().datas=menus;
        ServiceActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                binding.getAdapter().notifyDataSetChanged();
                setImageList();
            }
        });
    }


    /**
     * 图片逗号分割格式转数组
     * @param sou
     * @return
     */
    private List<String> string2List(String sou)
    {
        List<String> result=new ArrayList<>();
        if(!TextUtils.isEmpty(sou))
        {
            String[] temp=sou.split(",");
            for(String photo:temp)
            {
                result.add(photo);
            }
        }
        return result;
    }

    /**
     * 设置banner图片
     */
    private void setImageList() {

        BannerImageAdapter adapter = new BannerImageAdapter<DataBean>(getCurrentImageList()) {
            @Override
            public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {
                Bitmap temp = imageManager.loadImageToBitmap(data.imageUrl);
                Bitmap bitmap=imageManager.scale(temp,2560);
                holder.imageView.setImageBitmap(bitmap);
                holder.imageView.setLongClickable(true);
                holder.imageView.setClickable(true);
                holder.imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                holder.imageView.setTag(position);

                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        binding.setShowMenu(false);
                    }
                });

                holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position=new Integer(v.getTag().toString());
                        showBigImage(getCurrentImageList().get(position).imageUrl);
                        return false;
                    }
                });
            }
        };
        binding.serviceBanner.setAdapter(adapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(this));

    }

    /**
     * 获取当前菜单图片列表
     * @return
     */
    private List<DataBean> getCurrentImageList()
    {
        for(ServiceMenuItemBean item:binding.getAdapter().datas)
        {
            if(item.iscurrent)
            {
                List<DataBean> photos=new ArrayList<>();
                for(String file:item.photos)
                {
                    DataBean bean=new DataBean();
                    bean.imageUrl=file;
                    photos.add(bean);
                }
                return photos;
            }
        }
        return new ArrayList<>();
    }

    public void gotoHome()
    {
        this.finish();
    }

    public void onShowMenu(){
        binding.setShowMenu(true);
    }

    public void onHideMenu(){
        binding.setShowMenu(false);
    }

    public void showBigImage(String file)
    {
        Bitmap temp=imageManager.loadImageToBitmap(file);
        Bitmap bitmap=imageManager.scale(temp,5760);
        binding.serviceBigImage.setImageBitmap(bitmap);
        binding.setShowBigPic(true);
    }
    public void hideBigImage()
    {
        binding.setShowBigPic(false);
    }
}